package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.pojo.Massage;
import com.gxa.p2p.service.MassageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author R
 * @data 2021-10-26 10:20
 */
@CrossOrigin
@RestController
@RequestMapping("/massage")
@Api("消息控制层")
public class MassageController {

    @Autowired
    MassageService service = null;

    /**
     * 饶贵钢
     * @param page
     * @param size
     * @param title
     * @param state
     * @return
     */
    @PostMapping("/findMassageByPage")
    @ApiOperation("查询消息推送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "标题",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "状态",required = false,dataType = "boolean"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,dataType = "int")
    })
    public Map findMassageByPage(@RequestParam("page") Integer page,
                                 @RequestParam("size") Integer size,
                                 @RequestParam("title") String title,
                                 @RequestParam("state") Boolean state){
        List<Massage> massageByPage = service.findMassageByPage(title, state, page, size);
        Integer massageCount = service.findMassageCount(title, state, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        jsonResult.setData(massageByPage);
        jsonResult.put("count",massageCount);
        return jsonResult.getDataMap();
    }

    /**
     * 饶贵钢
     * @param massage
     * @return
     */
    @PostMapping("/addMassage")
    @ApiOperation("新增消息推送")
    @ApiImplicitParam(name = "massage",value = "消息详情",required = true,paramType = "body")
    public Map addMassage(Massage massage){
        service.addMassage(massage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
}
